package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteEmployeeRoleOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.EmployeeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.EmployeeCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.EmployeeService;
import com.cskaoyan.wordmemorize.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.v3.oas.annotations.Operation;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 员工管理控制器
 */
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private FileService fileService;

    /**
     * 获取当前登录用户信息
     *
     * @return 员工信息
     */
    @GetMapping("/info")
    @Operation(summary = "获取当前登录用户信息", description = "登录后获取当前用户信息，包含权限列表")
    public Result<EmployeeDTO> getEmployeeInfo() {
        // 调用服务层获取当前登录用户信息
        EmployeeDTO employeeDTO = employeeService.getEmployeeInfo();
        return Result.ok(employeeDTO);
    }

    /**
     * 获取后台员工列表
     *
     * @param pageRequest 分页查询参数
     * @return 分页员工列表
     */
    @GetMapping("/list")
    public Result<PageDTO<EmployeeDTO>> getEmployeeList(PageRequest pageRequest) {
        PageDTO<EmployeeDTO> pageDTO = employeeService.getEmployeePage(pageRequest);
        return Result.ok(pageDTO);
    }

    /**
     * 添加员工
     *
     * @param command 员工信息
     * @return 成功结果
     */
    @PostMapping
    public Result<Void> addEmployee(@RequestBody EmployeeCommand command) {
        employeeService.addEmployee(command);
        return Result.ok();
    }

    /**
     * 更新员工信息
     *
     * @param command 员工信息
     * @return 成功结果
     */
    @PutMapping
    public Result<Void> updateEmployee(@RequestBody EmployeeCommand command) {
        employeeService.updateEmployee(command);
        return Result.ok();
    }

    /**
     * 删除员工
     *
     * @param employeeId 员工ID
     * @return 成功结果
     */
    @DeleteMapping("/{employeeId}")
    public Result<Void> deleteEmployee(@PathVariable Long employeeId) {
        employeeService.delete(employeeId);
        return Result.ok();
    }

    /**
     * 上传员工头像
     *
     * @param file 头像文件
     * @return 头像URL
     */
    @PostMapping("/avatar")
    @Operation(summary = "上传头像", description = "主要用于账号权限管理-员工管理，添加或者编辑员工")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        try {
            // 上传头像到OSS，指定目录为avatar
            String avatarUrl = fileService.uploadFile(file, "avatar");

            return Result.ok(avatarUrl);
        } catch (IOException e) {
            Result<String> result = new Result<>();
            result.setCode(500);
            result.setMessage("头像上传失败: " + e.getMessage());
            return result;
        }
    }


    /**
     * 删除员工角色
     *
     * @param employeeId 员工ID
     * @param roleId     角色ID
     * @return 删除操作日志
     */
    @DeleteMapping("/{employeeId}/role/{roleId}")
    public Result<DeleteEmployeeRoleOperateDTO> deleteEmployeeRole(@PathVariable Long employeeId, @PathVariable Long roleId) {
        DeleteEmployeeRoleOperateDTO operateDTO = employeeService.deleteRole(employeeId, roleId);
        return Result.ok(operateDTO);
    }

    /**
     * 更新员工登录信息
     *
     * @param employeeId 员工ID
     * @return 成功结果
     */
    @PutMapping("/{employeeId}/login-info")
    public Result<Void> updateLoginInfo(@PathVariable Long employeeId) {
        // 这里应该从请求中获取IP，暂时写个默认的
        String ip = "127.0.0.1";
        employeeService.updateLoginInfo(employeeId, ip, LocalDateTime.now());
        return Result.ok();
    }
}
